var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var pay = require('../../../services/pay.js');

Page({
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {},
    queryBean:{},
    queryBeans: {},
    queryBeanStr:'',
    
    hour:'00',
    minute:'00',
    second:'00' 
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数status
    var that = this;
    if (options.id){
      this.setData({
        orderId: options.id
      });
    }
    var queryBean = JSON.parse(options.queryBean);
    that.setData({
      // queryBean: queryBean,
      orderId: queryBean.orderId,
      queryBeanStr: options.queryBean
    });
    //获取订单详情
    this.getOrderDetail();
    

   
  },

  getOrderDetail:function(){
    var that = this;
    var data = {};
    data.orderId = this.data.orderId;
    util.request(api.OrderDetail + this.data.orderId).then(function (res) {
      console.log("订单详情返回值")
      console.log(res);
      that.setData({
        queryBeans:res.responseBody,
        queryBean: res.responseBody //兼容水哥代码
      });

      that.showRemainingTime();
    });
  },

  showRemainingTime(){
    var that = this;
    var countDownTime = that.data.queryBean.countDownTime;
    if (countDownTime == null || countDownTime == '' || countDownTime == undefined){
      return;
    }
    var interval = setInterval(function () {
      var hour = Math.floor(countDownTime / 3600);
      if (hour<10){
        hour = "0" + hour;
      }
      var minute = Math.floor((countDownTime % 3600) / 60);
      if (minute<10){
        minute = "0" + minute;
      }
      var second = Math.floor(((countDownTime % 3600) % 60));
      if (second<10){
        second = "0" + second; 
      }
      that.setData({
        hour: hour,
        minute: minute,
        second: second,
        
      });
      
      countDownTime--;
      if (countDownTime < 0) {
        clearInterval(interval);
        // wx.showToast({
        //   title: '订单已关闭',
        // });
         
        that.setData({
          hour: '00',
          minute: '00',
          second: '00'
        });
      }  
    },1000);
  },

  cancel_c_Order:function(){
    //取消订单
    var orderNo = this.data.orderId;
    wx.showModal({
      title: '温馨提示',
      content: '确定取消订单',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          util.request(api.cancelOrder, { "orderNo": orderNo }, 'POST').then(function (res) {
            console.log(res);
            if (res.code == 200) {
              wx.showToast({
                title: '已取消',
                icon: 'loading',
                duration: 1000
              });
              wx.switchTab({
                url: '/pages/ucenter/index/index',
              })
            }
          })
        } else {
          console.log('用户点击取消')
        }

      }
    })
    
  },

  payOrder:function(){
    //去付款
    //支付
    var data = {};
    data.payMoney = Math.round(this.data.queryBean.principal * 100,0);
    data.body = this.data.queryBean.campaignName;
    data.detail = "";
    data.attach = "";
    data.orderNo = this.data.queryBean.orderId;

    console.log(data)
    pay.payOrder(data).then(function (res) {
      console.log(res);
      if (res.errMsg == "requestPayment:ok") {
        wx.navigateTo({
          url: '/pages/ucenter/order/order?tab=2'
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '支付失败，请稍后重试',
          showCancel : false
        })
      }
    }).catch(function (res) {
      console.log(res)
      wx.showModal({
        title: '提示',
        content: '支付失败，请稍后重试',
        showCancel: false
      })
    })
  } , 

  evaluate:function(){
    //去评价
    var that = this;
    wx.navigateTo({
      url: '/pages/ucenter/evaluate/evaluate?queryBeanStr=' + that.data.queryBeanStr
    })
  },

  showGoods:function(e){
    //展示活动详情
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + e.currentTarget.id
    })
  },

  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})